package com.sky.financelake.retrofit;

import androidx.lifecycle.MutableLiveData;

import com.sky.financelake.bean.dto.RPlateZjlxDto;
import com.sky.financelake.bean.dto.SimplePlateZjlxDto;
import com.sky.financelake.bean.dto.SimpleStockZjlxDto;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ZjlxRepository {

    private static ZjlxRepository zjlxRepository;

    public static ZjlxRepository getInstance() {
        if (zjlxRepository == null) {
            zjlxRepository = new ZjlxRepository();
        }
        return zjlxRepository;
    }

    private Api api;

    public ZjlxRepository() {
        api = RetrofitService.cteateService(Api.class);
    }

    public void getPlateZjlxRank(MutableLiveData data, Integer days) {
        api.getPlateZjlxRank(days).enqueue(new Callback<ApiResponse<List<RPlateZjlxDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<RPlateZjlxDto>>> call, Response<ApiResponse<List<RPlateZjlxDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<RPlateZjlxDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }

    public void getStockZjlx(MutableLiveData data, String key) {
        api.getStockZjlx(key).enqueue(new Callback<ApiResponse<List<SimpleStockZjlxDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SimpleStockZjlxDto>>> call, Response<ApiResponse<List<SimpleStockZjlxDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SimpleStockZjlxDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }


    public void getPlateZjlx(MutableLiveData data, String key) {
        api.getPlateZjlx(key).enqueue(new Callback<ApiResponse<List<SimplePlateZjlxDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<SimplePlateZjlxDto>>> call, Response<ApiResponse<List<SimplePlateZjlxDto>>> response) {
                if (response.isSuccessful()) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<SimplePlateZjlxDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }




}
